package com.zyf.chapter02.structure.proxy;

import com.zyf.chapter01.practice.v3.MetricsCollector;
import com.zyf.chapter01.principle.practice.RequestInfo;

/**
 * 静态代理案例
 * 该写法存在的问题：
 *  1.侵入业务代码，与业务代码高度耦合，且替换框架的成本较大
 *  2.业务类最好职责更加单一，只聚焦业务处理
 * @author yifeng
 */
public class UserController {
    // 依赖注入
    private MetricsCollector metricsCollector;

    public UserVo login(String telephone, String password) {
        long startTimestamp = System.currentTimeMillis();

        // ... 省略 login 逻辑...

        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        RequestInfo requestInfo = new RequestInfo("login", responseTime, startTimestamp);
        metricsCollector.recordRequest(requestInfo);

        // 返回 UserVo
        return null;
    }

    public UserVo register(String telephone, String password) {
        long startTimestamp = System.currentTimeMillis();

        // ... 省略 register 逻辑...

        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;

        RequestInfo requestInfo = new RequestInfo("register", responseTime, startTimestamp);
        metricsCollector.recordRequest(requestInfo);

        // ...返回 UserVo 数据...
        return null;
    }
}
